<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
class CatController extends Controller
{
    /**
     * [栏目列表页]
     * @param  Request $req    [Request对象]
     * @param  [int]  $cat_id  [栏目id]
     * @return [page]          [栏目页面]
     */
    public function cat(Request $req,$cat_id)
    {
    //通过传过来的cat_id去查询他的parentid
    $parentid=DB::table('cats')->select('cat_id','parentid')->where('cat_id',$cat_id)->first();
    if($cat_id == 1 || $cat_id == 2){
        //查出所有的栏目
        $catlist=DB::table('cats')->select('cat_id','cat_name','parentid')->where('cat_status',1)->where('parentid',$cat_id)->get();
        //查询出所有此栏目下的子栏目
        $catname=DB::table('cats')->select('cat_id','cat_name')->where('parentid',$cat_id)->get();
    }else{
        //通过查出的parentid去查询他的父栏目的所有栏目
        $catlist=DB::table('cats')->select('cat_id','cat_name','parentid')->where('cat_status',1)->where('cat_id',$cat_id)->where('parentid',$parentid->parentid)->get();
        //用查询出的parentid,去查询所有与他父级的栏目
        $catname=DB::table('cats')->select('cat_id','cat_name')->where('parentid',$parentid->parentid)->get();
    }
        //查询文章表的所有内容
        $artlist=DB::table('arts')->select('art_id','cat_id','art_title','art_img','art_intro','art_pubtime','art_click')->where('art_status',1)->get(4);
      return view('home.cat',['artlist'=>$artlist,'catlist'=>$catlist,'catname'=>$catname]);
    
    }

}
